<?php
/**
 * 后台角色管理
 *
 * 后台角色管理controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Backstage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Role;
use App\Permission;
use App\Services\PermissionsService\PermissionsService;
use DB;

class RolesController extends Controller
{
    public function __construct(PermissionsService $permissionsService)
    {
        $this->permission_service = $permissionsService;
    }

    /**
     * 角色列表
     * @return $this
     */
    public function getList(){
        $roles = Role::paginate(15);
        return view('backstage.roles.list')
            ->with('roles',$roles);
    }

    /**
     * 角色添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getAdd(){
        return view('backstage.roles.add');
    }

    /**
     * 角色添加
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAdd(Request $request){
        $this->validate($request,[
            'name' => 'required|alpha_dash|max:20|unique:roles,name',
            'display_name' => 'required|max:20|unique:roles,display_name',
            'description' => 'max:200'
        ]);
        $name = trim($request->input('name'));
        $display_name = trim($request->input('display_name'));
        $description = trim($request->input('description'));
        $role = new Role;
        $role->name = $name;
        $role->display_name = $display_name;
        $role->description = $description;
        $role->save();
        return redirect()->back()->with('success_message','添加成功');
    }

    /**
     * 更新页面
     * @param $id
     * @return $this
     */
    public function getUpdate($id){
        $role = Role::find(intval($id));
        return view('backstage.roles.update')
            ->with('role',$role);
    }

    /**
     * 更新角色
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postUpdate(Request $request){
        $this->validate($request,[
            'id' => 'required|integer',
            'name' => 'required|alpha_dash|max:20|unique:roles,name,'.intval($request->input('id')).',id',
            'display_name' => 'required|max:20|unique:roles,display_name,'.intval($request->input('id')).',id',
            'description' => 'max:200'
        ]);
        $name = trim($request->input('name'));
        $display_name = trim($request->input('display_name'));
        $description = trim($request->input('description'));
        $role = Role::find(intval($request->input('id')));
        $role->name = $name;
        $role->display_name = $display_name;
        $role->description = $description;
        $role->save();
        return redirect('backstage/roles/list')->with('success_message','更新成功');
    }

    /**
     * 添加权限页面
     * @param $id
     * @return mixed
     */
    public function getAddPermission($id){

        //$permissions = Permission::all();
        $permission_select_ids = DB::table('permission_role')->where('role_id',intval($id))->pluck('permission_id')->toArray();
//        $permissionArr = array();
//        foreach($permissions as $permission){
//            $permission->selected = in_array($permission->id,$permission_select_ids) ? 1 : 0;
//            $permissionArr[$permission->description][] = $permission;
//        }
        $permissions = $this->permission_service->getRolePermissionList();
        //dd($permissions);
        return view('backstage.roles.add_permission')
            ->with('permission_select_ids',$permission_select_ids)
            ->with('permissions',$permissions)
            ->with('id',intval($id));
    }

    /**
     * 添加权限
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAddPermission(Request $request){
        $permission_ids = $request->input('permission_ids');
        if(empty($permission_ids)){
            return redirect()->back()->with('error_message','请至少选择一个权限');
        }
        $role = Role::find(intval($request->input('id')));
        $role->perms()->sync($permission_ids);
        return redirect()->back()->with('success_message','添加成功');
    }

    /**
     * 删除
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function getDelete($id){
        //$role = Role::find(intval($id));
        Role::where('id', intval($id))->delete();
        //删除权限
        DB::table('permission_role')->where('role_id','=',intval($id))->delete();
        //删除用户
        DB::table('role_user')->where('role_id','=',intval($id))->delete();
        return redirect()->back()->with('success_message','删除成功');
    }
}
